// Fill out your copyright notice in the Description page of Project Settings.


#include "AI/TUAIController.h"
#include "AI/TUAICharacter.h"
#include "Component/TUAIPerceptionComponent.h"
#include "Component/TURespawnComponent.h"
#include "BehaviorTree/BlackboardComponent.h"

ATUAIController::ATUAIController()
{
    TUAIPerceptionComponent = CreateDefaultSubobject<UTUAIPerceptionComponent>("TUPerceptionComponent");
    SetPerceptionComponent(*TUAIPerceptionComponent);

    RespawnComponent = CreateDefaultSubobject<UTURespawnComponent>("RespawnComponent");

    bWantsPlayerState = true;
}

void ATUAIController::OnPossess(APawn* InPawn)
{
    Super::OnPossess(InPawn);

    if (const auto TUCharacter = Cast<ATUAICharacter>(InPawn))
    {
        RunBehaviorTree(TUCharacter->BehaviorTreeAsset);
    }
}

void ATUAIController::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
 
    SetFocus(GetFocusOnActor());
}

AActor* ATUAIController::GetFocusOnActor() const
{
    if (!GetBlackboardComponent()) return nullptr;
    return Cast<AActor>(GetBlackboardComponent()->GetValueAsObject(FocusOnKeyName));
}
